All Exams ()
0): ?>No Exams Created Yet
Create your first exam to get started with the CBT system.
beginTransaction(); // Insert exam $stmt = $pdo->prepare("INSERT INTO exams (name, year, duration, status, min_subjects, max_subjects) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $year, $duration, $status, $min_subjects, $max_subjects]); $exam_id = $pdo->lastInsertId(); // Insert exam subjects foreach ($selected_subjects as $subject_id) { $stmt = $pdo->prepare("INSERT INTO exam_subjects (exam_id, subject_id) VALUES (?, ?)"); $stmt->execute([$exam_id, $subject_id]); } $pdo->commit(); $success = "Exam created successfully with " . count($selected_subjects) . " subjects!"; } catch (PDOException $e) { $pdo->rollBack(); $error = "Error creating exam: " . $e->getMessage(); } } // Handle exam update if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_exam'])) { $exam_id = sanitize_input($_POST['exam_id']); $name = sanitize_input($_POST['name']); $year = sanitize_input($_POST['year']); $duration = sanitize_input($_POST['duration']); $status = sanitize_input($_POST['status']); $min_subjects = sanitize_input($_POST['min_subjects']); $max_subjects = sanitize_input($_POST['max_subjects']); $selected_subjects = $_POST['subjects'] ?? []; try { $pdo->beginTransaction(); // Update exam $stmt = $pdo->prepare("UPDATE exams SET name = ?, year = ?, duration = ?, status = ?, min_subjects = ?, max_subjects = ? WHERE id = ?"); $stmt->execute([$name, $year, $duration, $status, $min_subjects, $max_subjects, $exam_id]); // Update exam subjects $stmt = $pdo->prepare("DELETE FROM exam_subjects WHERE exam_id = ?"); $stmt->execute([$exam_id]); foreach ($selected_subjects as $subject_id) { $stmt = $pdo->prepare("INSERT INTO exam_subjects (exam_id, subject_id) VALUES (?, ?)"); $stmt->execute([$exam_id, $subject_id]); } $pdo->commit(); $success = "Exam updated successfully!"; } catch (PDOException $e) { $pdo->rollBack(); $error = "Error updating exam: " . $e->getMessage(); } } // Handle exam deletion if (isset($_GET['delete_id'])) { $delete_id = sanitize_input($_GET['delete_id']); try { // Check if exam has questions $stmt = $pdo->prepare("SELECT COUNT(*) as question_count FROM questions WHERE exam_id = ?"); $stmt->execute([$delete_id]); $result = $stmt->fetch(PDO::FETCH_ASSOC); if ($result['question_count'] > 0) { $error = "Cannot delete exam that has questions. Please delete the questions first."; } else { $pdo->beginTransaction(); // Delete exam subjects $stmt = $pdo->prepare("DELETE FROM exam_subjects WHERE exam_id = ?"); $stmt->execute([$delete_id]); // Delete exam $stmt = $pdo->prepare("DELETE FROM exams WHERE id = ?"); $stmt->execute([$delete_id]); $pdo->commit(); $success = "Exam deleted successfully!"; } } catch (PDOException $e) { $pdo->rollBack(); $error = "Error deleting exam: " . $e->getMessage(); } } // Get all exams with subject counts $stmt = $pdo->prepare(" SELECT e.*, COUNT(DISTINCT es.subject_id) as subject_count, (SELECT COUNT(*) FROM questions WHERE exam_id = e.id) as question_count, (SELECT COUNT(*) FROM exam_sessions WHERE exam_id = e.id) as attempt_count FROM exams e LEFT JOIN exam_subjects es ON e.id = es.exam_id GROUP BY e.id ORDER BY e.year DESC, e.name "); $stmt->execute(); $exams = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get all subjects $stmt = $pdo->prepare("SELECT * FROM subjects ORDER BY name"); $stmt->execute(); $subjects = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get exam for editing $edit_exam = null; $exam_subjects = []; if (isset($_GET['edit_id'])) { $edit_id = sanitize_input($_GET['edit_id']); $stmt = $pdo->prepare("SELECT * FROM exams WHERE id = ?"); $stmt->execute([$edit_id]); $edit_exam = $stmt->fetch(PDO::FETCH_ASSOC); // Get exam subjects if ($edit_exam) { $stmt = $pdo->prepare("SELECT subject_id FROM exam_subjects WHERE exam_id = ?"); $stmt->execute([$edit_id]); $exam_subjects = $stmt->fetchAll(PDO::FETCH_COLUMN); } } ?>
Create, edit, and manage examination schedules with multiple subjects
Create your first exam to get started with the CBT system.